package Tools.Crop
{
	import com.catalog.Event.CropEvent;
	
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import mx.core.Container;
	import mx.core.UIComponent;
	import mx.events.FlexEvent;
	import mx.graphics.SolidColor;
	import mx.graphics.SolidColorStroke;
	
	import spark.components.Group;
	import spark.components.supportClasses.SkinnableComponent;
	import spark.primitives.Rect;
	
	[Event(name="crop",type="com.catalog.Event.CropEvent")]
	public class CropComponent extends Group
	{
		public function CropComponent()
		{
			super();
			initProperties();
			initChildren();
		}
		
		private var _topLeftPoint:Point;
		
		private var _bottomRightPoint:Point;
		
		private var _parentRecComponent:UIComponent;
		
		private var topLeftCircle:CropCircleButton;
		private var topRightCircle:CropCircleButton;
		private var bottomLeftCircle:CropCircleButton;
		private var bottomRightCircle:CropCircleButton;
		private var cropRec:CropRectComponent;
		
		public function get parentRecComponent():UIComponent
		{
			return _parentRecComponent;
		}
		
		public function set parentRecComponent(value:UIComponent):void
		{
			_parentRecComponent = value;
			_parentRecComponent.addEventListener(FlexEvent.UPDATE_COMPLETE,onParentUpdateComplete);
		}
		
		private function onParentUpdateComplete(event:FlexEvent):void
		{
			resetCropSize();
		}
		
		public function get bottomRightPoint():Point
		{
			return _bottomRightPoint;
		}

		public function set bottomRightPoint(value:Point):void
		{
			_bottomRightPoint = value;
			invalidateDisplayList();
		}

		public function get topLeftPoint():Point
		{
			return _topLeftPoint;
		}

		public function set topLeftPoint(value:Point):void
		{
			_topLeftPoint = value;
			invalidateDisplayList();
		}
		
		override protected function createChildren():void
		{
			super.createChildren();
			addElement( cropRec );
			addElement( topLeftCircle );
			addElement( topRightCircle );
			addElement( bottomLeftCircle );
			addElement( bottomRightCircle );
			
			registerEventListener( topLeftCircle );
			registerEventListener( topRightCircle );
			registerEventListener( bottomLeftCircle );
			registerEventListener( bottomRightCircle );
			registerEventListener( cropRec );
			
			/**监听双击事件，提交截图区域*/
			doubleClickEnabled = true;
			addEventListener(MouseEvent.DOUBLE_CLICK,onCropHandler );
		}
		
		override protected function measure():void
		{
			super.measure();
		}
		
		override protected function commitProperties():void
		{
			super.commitProperties();
		}
		
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{
			super.updateDisplayList(unscaledWidth,unscaledHeight);
			draw();
			lay();
		}
		
		/**
		 *  画图 
		 * 
		 */		
		private function draw():void
		{
			cropRec.topLeftPoint = topLeftPoint;
			cropRec.bottomRightPoint = bottomRightPoint;
			cropRec.parentRec = parentRecComponent.getRect(parentRecComponent.parent);
//			cropRec.graphics.clear();
//			cropRec.graphics.beginFill(0xcccccc,.5);
//			cropRec.graphics.lineStyle(1,0x000000);
//			cropRec.graphics.drawRect(topLeftPoint.x,topLeftPoint.y,bottomRightPoint.x - topLeftPoint.x,bottomRightPoint.y - topLeftPoint.y);
//			cropRec.graphics.endFill();
//			var cropRecFill:SolidColor = new SolidColor(0xcccccc,.5);
//			cropRec.fill = cropRecFill;
//			
//			var cropRecBorder:SolidColorStroke = new SolidColorStroke(0x000000);
//			cropRec.stroke = cropRecBorder;
//			
//			cropRec.x = topLeftPoint.x;
//			cropRec.y = topLeftPoint.y;
//			cropRec.width = bottomRightPoint.x - topLeftPoint.x;
//			cropRec.height = bottomRightPoint.y - topLeftPoint.y;
		}
		/**
		 * 放置四个圆圈位置 
		 * 
		 */		
		private function lay():void
		{
			topLeftCircle.move(topLeftPoint.x,topLeftPoint.y);
			topRightCircle.move(bottomRightPoint.x,topLeftPoint.y);
			bottomLeftCircle.move(topLeftPoint.x,bottomRightPoint.y);
			bottomRightCircle.move(bottomRightPoint.x,bottomRightPoint.y);
		}
		
		private function initProperties():void
		{
			topLeftPoint = new Point();
			bottomRightPoint = new Point();
		}
		
		private function initChildren():void
		{
			topLeftCircle = new CropCircleButton();
			topRightCircle = new CropCircleButton();
			bottomLeftCircle = new CropCircleButton();
			bottomRightCircle = new CropCircleButton();
			cropRec = new CropRectComponent();
		}
		
		private function resetCropSize():void
		{
			var parentRec:Rectangle = new Rectangle(parentRecComponent.x,parentRecComponent.y,parentRecComponent.width,parentRecComponent.height);
			topLeftPoint = new Point(parentRec.x+parentRec.width*3/20,parentRec.y+parentRec.height*3/20);
			bottomRightPoint = new Point(parentRec.x+parentRec.width-parentRec.width*3/20,parentRec.y+parentRec.height-parentRec.height*3/20);
		}
		
		private function registerEventListener(component:UIComponent):void
		{
			component.addEventListener( MouseEvent.MOUSE_DOWN,onMouseDownHandler );	
		}
		
		private var startPoint:Point;
		private var currentSelectCircle:UIComponent;
		private function onMouseDownHandler( event:MouseEvent ):void
		{
			stage.addEventListener( MouseEvent.MOUSE_MOVE,onMouseMoveHandler );
			stage.addEventListener( MouseEvent.MOUSE_UP,onMouseUpHandler );
			startPoint = new Point(event.stageX,event.stageY);
			currentSelectCircle = event.currentTarget as UIComponent;
		}
		
		private function onMouseMoveHandler( event:MouseEvent ):void
		{
			var endPoint:Point = new Point(event.stageX,event.stageY);
			caculatePoint(startPoint.clone(),endPoint.clone());
			startPoint = endPoint.clone();
		}
		
		private function onMouseUpHandler( event:MouseEvent ):void
		{
			stage.removeEventListener( MouseEvent.MOUSE_MOVE,onMouseMoveHandler );
		}
		
		/**
		 * 计算新的topLeftPoint,bottomRightPoint坐标 
		 * @param startPoint
		 * @param endPoint
		 * 
		 */		
		private function caculatePoint(startPoint:Point,endPoint:Point):void
		{				
			var moveX:Number = endPoint.x-startPoint.x;
			var moveY:Number = endPoint.y-startPoint.y;
			
			if( currentSelectCircle == topLeftCircle )
			{
//				topLeftPoint = new Point( topLeftPoint.x+moveX,topLeftPoint.y+moveY);
				if( topLeftPoint.x+moveX > 0 && topLeftPoint.x+moveX < parentRecComponent.width )
				{
					topLeftPoint.x = topLeftPoint.x+moveX;
				}
				if( topLeftPoint.y + moveY > 0 && topLeftPoint.y + moveY < parentRecComponent.height )
				{
					topLeftPoint.y = topLeftPoint.y+moveY;
				}
				
			}
			if( currentSelectCircle == topRightCircle )
			{
//				topLeftPoint = new Point(topLeftPoint.x,topLeftPoint.y+moveY);
//				bottomRightPoint = new Point(bottomRightPoint.x+moveX,bottomRightPoint.y);
				if( topLeftPoint.y+moveY > 0 && topLeftPoint.y+moveY < parentRecComponent.height )
				{
					topLeftPoint.y = topLeftPoint.y+moveY;
				}
				if( bottomRightPoint.x+moveX > 0 && bottomRightPoint.x+moveX < parentRecComponent.width )
				{
					bottomRightPoint.x = bottomRightPoint.x+moveX;
				}
			}
			if( currentSelectCircle == bottomLeftCircle )
			{
//				topLeftPoint = new Point(topLeftPoint.x+moveX,topLeftPoint.y);
//				bottomRightPoint = new Point(bottomRightPoint.x,bottomRightPoint.y+moveY);
				if( topLeftPoint.x+moveX > 0 && topLeftPoint.x+moveX < parentRecComponent.width )
				{
					topLeftPoint.x = topLeftPoint.x+moveX;
				}
				if( bottomRightPoint.y+moveY > 0 && bottomRightPoint.y+moveY < parentRecComponent.height )
				{
					bottomRightPoint.y = bottomRightPoint.y+moveY;
				}
			}
			if( currentSelectCircle == bottomRightCircle )
			{
//				bottomRightPoint = new Point(bottomRightPoint.x+moveX,bottomRightPoint.y+moveY);
				if( bottomRightPoint.x+moveX > 0 && bottomRightPoint.x+moveX < parentRecComponent.width )
				{
					bottomRightPoint.x = bottomRightPoint.x+moveX;
				}
				if( bottomRightPoint.y+moveY > 0 && bottomRightPoint.y+moveY < parentRecComponent.height )
				{
					bottomRightPoint.y = bottomRightPoint.y+moveY;
				}
			}
			if( currentSelectCircle == cropRec )
			{
//				topLeftPoint = new Point(topLeftPoint.x+moveX,topLeftPoint.y+moveY);
//				bottomRightPoint = new Point(bottomRightPoint.x+moveX,bottomRightPoint.y+moveY);
				var topLeftClonePoint:Point = topLeftPoint.clone();
				var topLeftCropPoint:Point = new Point(Math.min(topLeftClonePoint.x,bottomRightPoint.x),Math.min(topLeftClonePoint.y,bottomRightPoint.y));
				var bottomRightCropPoint:Point = new Point(Math.max(topLeftClonePoint.x,bottomRightPoint.x),Math.max(topLeftClonePoint.y,bottomRightPoint.y))
	
				if( (moveX > 0 && moveX < parentRecComponent.width - bottomRightCropPoint.x) || (moveX < 0 && Math.abs(moveX)<topLeftCropPoint.x) )
				{
					topLeftPoint.x+=moveX;
					bottomRightPoint.x+=moveX;
				}
				if( (moveY > 0 && moveY < parentRecComponent.height - bottomRightCropPoint.y) || ( moveY < 0 && Math.abs(moveY)<topLeftCropPoint.y) )
				{
					topLeftPoint.y+=moveY;
					bottomRightPoint.y+=moveY;
				}
//				if( topLeftCropPoint.x+moveX > 0 && topLeftCropPoint.x+moveX < parentRecComponent.width )
//				{
//					topLeftPoint.x = topLeftPoint.x + moveX;
//				}
//				if( topLeftCropPoint.y+moveY > 0 && topLeftCropPoint.y+moveY < parentRecComponent.height )
//				{
//					topLeftPoint.y = topLeftPoint.y + moveY;
//				}
//				if( bottomRightCropPoint.x+moveX > bottomRightCropPoint.x-topLeftCropPoint.x && bottomRightCropPoint.x+moveX < parentRecComponent.width )
//				{
//					bottomRightPoint.x = bottomRightPoint.x+moveX;
//				}
//				if( bottomRightCropPoint.y+moveY > bottomRightCropPoint.y - topLeftCropPoint.x && bottomRightCropPoint.y+moveY < parentRecComponent.height )
//				{
//					bottomRightPoint.y = bottomRightPoint.y+moveY ;
//				}
			}
			topLeftPoint = new Point(topLeftPoint.x,topLeftPoint.y);
			bottomRightPoint = new Point(bottomRightPoint.x,bottomRightPoint.y);
//			topLeftPoint = new Point(Math.max(parentRecComponent.x,topLeftPoint.x),Math.max(parentRecComponent.y,topLeftPoint.y));
//			bottomRightPoint = new Point(Math.min(parentRecComponent.x+parentRecComponent.width,bottomRightPoint.x),Math.min(parentRecComponent.y+parentRecComponent.height,bottomRightPoint.y));
		}
		
		private function onCropHandler( event:MouseEvent ):void
		{
			var rec:Rectangle = new Rectangle(Math.min(topLeftPoint.x,bottomRightPoint.x),Math.min(topLeftPoint.y,bottomRightPoint.y),Math.abs(topLeftPoint.x-bottomRightPoint.x),Math.abs(topLeftPoint.y-bottomRightPoint.y));
			var cropEvent:CropEvent = new CropEvent(CropEvent.CROP);
			cropEvent.cropRec = rec;
			dispatchEvent( cropEvent );
		}
	}
}